//
//  DateTimePickerViewController.m
//  Uhomebk
//
//  Created by XXJ on 15/12/21.
//  Copyright © 2015年 com.segi. All rights reserved.
//

#import "DateTimePickerViewController.h"

@interface DateTimePickerViewController ()
{
    UIView *boxView;
    UIDatePicker *datePickerVC;
}
@end

@implementation DateTimePickerViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    @synchronized(self)
    {
        [UIView animateWithDuration:0.3 animations:^{
            boxView.transform = CGAffineTransformMakeTranslation(0, 0);
        }];
    }
    
}

-(instancetype)init
{
    self = [super init];
    if (self) {
        self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
            self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
        }else{
            self.modalPresentationStyle=UIModalPresentationCurrentContext;
        }
        
        boxView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds)-200, CGRectGetWidth(self.view.bounds), 200)];
        boxView.transform = CGAffineTransformMakeTranslation(0, 200);
        boxView.backgroundColor = kColor_W;
        [self.view addSubview:boxView];
        
        //关闭按钮和确定按钮
        UIView *btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(boxView.bounds), 40)];
        btnView.backgroundColor = [UIColor seg_setColorWithAny:kColor_bg dark:kColor_W2];
        [boxView addSubview:btnView];
        
        //关闭
        CustomButtonView *closeBtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(5, 5, 60, 30)];
        [closeBtn setTitle:@"取消" forState:UIControlStateNormal];
        closeBtn.tag = 100;
        closeBtn.layer.cornerRadius = 3;
        [closeBtn.layer setMasksToBounds:YES];
        [closeBtn setTitleColor:kColor_1 forState:UIControlStateNormal];
        [closeBtn.titleLabel setFont:kFont_16];
        [closeBtn addTarget:self action:@selector(onCloseBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnView addSubview:closeBtn];
        
        //确定
        CustomButtonView *submitBtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds)-60, 5, 60, 30)];
        submitBtn.tag = 101;
        submitBtn.layer.cornerRadius = 3;
        [submitBtn.layer setMasksToBounds:YES];
        [submitBtn setTitle:@"确定" forState:UIControlStateNormal];
        [submitBtn setTitleColor:kColor_1 forState:UIControlStateNormal];
        [submitBtn.titleLabel setFont:kFont_16];
        [submitBtn addTarget:self action:@selector(onCloseBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnView addSubview:submitBtn];
        
        datePickerVC = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, 40, CGRectGetWidth(boxView.bounds)-20, CGRectGetHeight(boxView.bounds)-40)];
        if (self.isModeDate) {
            datePickerVC.datePickerMode = UIDatePickerModeDate;
        }else{
            datePickerVC.datePickerMode = UIDatePickerModeDateAndTime;
        }
        if (@available(iOS 13.4, *)) {
            datePickerVC.preferredDatePickerStyle = UIDatePickerStyleWheels;
        }
        //    datePickerVC.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
        [datePickerVC setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh"]];
        [boxView addSubview:datePickerVC];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 40-0.5, CGRectGetWidth(self.view.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        [btnView addSubview:lineView];
    }
    return self;
}

-(void)setIsModeDate:(BOOL)isModeDate
{
    _isModeDate = isModeDate;
    if (isModeDate) {
        datePickerVC.datePickerMode = UIDatePickerModeDate;
    }else{
        datePickerVC.datePickerMode = UIDatePickerModeDateAndTime;
    }
}

-(void)onCloseBtnAction:(CustomButtonView *)sender
{
    if (sender.tag==100)
    {
        @synchronized(self)
        {
            [UIView animateWithDuration:0.1 animations:^{
                boxView.transform = CGAffineTransformMakeTranslation(0, 200);
                
            } completion:^(BOOL finished) {
                [self dismissViewControllerAnimated:YES completion:^{
                    //
                }];
            }];
        }
    }
    else
    {
        @synchronized(self)
        {
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
//            [dateFormatter setTimeZone:NSDateFormatterNoStyle];
            [dateFormatter setLocale:[NSLocale currentLocale]];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *dateStr = [dateFormatter stringFromDate:[datePickerVC date]];
            
            [self.fundelegate onDateTimePickerValue:dateStr];
            [UIView animateWithDuration:0.1 animations:^{
                boxView.transform = CGAffineTransformMakeTranslation(0, 200);
                
            } completion:^(BOOL finished) {
                [self dismissViewControllerAnimated:YES completion:^{
                    //
                }];
            }];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
